//
//  WKViewExtension.swift
//  商家端Swift
//
//  Created by 天下宅 on 16/8/17.
//  Copyright © 2016年 天下宅. All rights reserved.
//

import Foundation
import Toast_Swift
import SnapKit
struct ViewLineOptions: OptionSet {
    let rawValue: Int
    
    static let top = ViewLineOptions(rawValue: 1 << 0)
    static let mid = ViewLineOptions(rawValue: 1 << 1)
    static let bottom = ViewLineOptions(rawValue: 1 << 2)
    static let left = ViewLineOptions(rawValue: 1 << 3)
    static let right = ViewLineOptions(rawValue: 1 << 4)

}

extension UIView
{
    func addLine(WithColor color: UIColor, model:ViewLineOptions)
    {
        
        func line(WithClock color: UIColor) -> UIView
        {
            let v = UIView()
            v.backgroundColor = color
            self.addSubview(v)
            return v
        }
        
        
        if model.contains(.top) {
            let v = line(WithClock: color)
            v.snp.makeConstraints({ (make) in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(0.5)
            })
        }
        
        if model.contains(.mid){
            let v = line(WithClock: color)
            v.snp.makeConstraints({ (make) in
                make.left.equalTo(self).offset(10)
                make.right.centerY.equalToSuperview()
                make.height.equalTo(0.5)
            })
        }
        
        if model.contains(.bottom)
        {
            let v = line(WithClock: color)
            v.snp.makeConstraints({ (make) in
                make.bottom.left.right.equalToSuperview()
                make.height.equalTo(0.5)
            })
        }
        
        if model.contains(.left)
        {
            let v = line(WithClock: color)
            v.snp.makeConstraints({ (make) in
                make.bottom.left.top.equalToSuperview()
                make.width.equalTo(0.5)
            })
        }
        
        if model.contains(.right)
        {
            let v = line(WithClock: color)
            v.snp.makeConstraints({ (make) in
                make.bottom.top.right.equalToSuperview()
                make.width.equalTo(0.5)
            })
        }
    }
    
    
}

//更改Toast背景色
extension UIView
{
    
    
    public func wkMakeToastToCenter(message: String) {

        
        self.makeToast(message, duration: ToastManager.shared.duration, position: ToastPosition.center )
    }
    
    public func wkMakeToast(message: String) {
        //        wkStyle.backgroundColor = UIColor.init(red: 1, green: 171 / 255.0, blue: 82/255.0, alpha: 1).colorWithAlphaComponent(0.6)
        
        self.makeToast(message, duration: ToastManager.shared.duration, position: ToastManager.shared.position)
    }

    
    
}
//view frame属性快速获取
extension UIView
{
    var left:CGFloat{
        set{
            self.frame.origin.x = newValue
        }
        get{
            return self.frame.origin.x
        }
    }
    
    var top:CGFloat{
        set{
            self.frame.origin.y = newValue
        }
        get{
            return self.frame.origin.y
        }
    }
    
    var right:CGFloat{
        
        get{
            return self.frame.maxX
        }
    }
    
    var bottom:CGFloat{
        get{
            return self.frame.maxY
        }
    }
    
    var midX:CGFloat{
        get{ return self.frame.midX }
    }
    
    var midY:CGFloat{
        get{ return self.frame.midY }
    }
    
    var width:CGFloat{
        set{
            let frame = CGRect(x:self.left, y:self.top, width:newValue, height:self.frame.height)
            self.frame = frame
        }
        
        get{
            return self.frame.width
        }
    }
    
    
    var height:CGFloat{
        set{
            let frame = CGRect(x:self.left, y:self.top, width:self.frame.width, height:newValue)
            self.frame = frame
        }
        get{
            return self.frame.height
        }
    }
    
    var origin:CGPoint{
        set{
            self.frame.origin = newValue
        }
        
        get{
            return self.frame.origin
        }
    }
    
    var size:CGSize{
        set{
            self.frame.size = newValue
        }
        
        get{
            return self.frame.size
        }
    }
    
    
}

//自带 关闭编辑模式属性
extension UIView
{
    
    open override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        self.endEditing(true)
    }
}
